package cn.htsyb.synergy.maker.util;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class FileUtil {

    public static void existAndCreate(File file) throws IOException {
        if (!file.exists()) {
            file.getParentFile().mkdirs();
            file.createNewFile();
        }
    }

    /**
     * 列取文件列表
     *
     * @param baseFilePath
     * @param endWith
     * @return
     */
    public static List<File> listFile(String baseFilePath, String endWith) {
        return listFile(new File(baseFilePath), endWith);
    }

    /**
     * 列取文件列表
     *
     * @param baseFile
     * @param endWith
     * @return
     */
    public static List<File> listFile(File baseFile, String endWith) {

        List<File> fileList = new ArrayList<File>();
        if (baseFile != null) {
            if (baseFile.isFile()) {
                if (baseFile.getName().endsWith(endWith)) {
                    fileList.add(baseFile);
                }
            } else {
                for (File file : baseFile.listFiles()) {
                    fileList.addAll(listFile(file, endWith));
                }
            }
        }
        return fileList;
    }
}
